"""
Case Type   : PURGE
Case Name   : purge recyclebin
Create At   : 2022-09-15
Owner       : liu-tong-8848
Description :
    1、设置参数并重启数据库
    2、创建表
    3、创建索引
    4、删除表
    5、查询回收站
    6、purge
    7、查询回收站
    8、清理环境
Expect      :
    1、设置参数并重启数据库，expect: 成功
    2、创建表，expect: 成功
    3、创建索引，expect: 成功
    4、删除表，expect: 成功
    5、查询回收站，expect: 成功
    6、purge，expect: 成功
    7、查询回收站，expect: 成功
    8、清理环境，expect: 成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common


class Purge(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.t_name = 't_purge_0007'
        self.i_name = 'i_purge_0007'
        self.default_value1 = self.common.show_param('enable_recyclebin')
        self.default_value2 = self.common.show_param(
            'enable_default_ustore_table')

    def test_main(self):
        step1_txt = '-----step1:设置参数并重启数据库; expect:成功-----'
        self.log.info(step1_txt)
        msg1 = self.pri_sh.execute_gsguc(
            "set", self.constant.GSGUC_SUCCESS_MSG, "enable_recyclebin='on'")
        self.log.info(msg1)
        self.assertTrue(msg1, '执行失败:' + step1_txt)
        msg1_1 = self.pri_sh.execute_gsguc(
            "set", self.constant.GSGUC_SUCCESS_MSG,
            "enable_default_ustore_table='on'")
        self.log.info(msg1_1)
        self.assertTrue(msg1_1, '执行失败:' + step1_txt)
        restart_msg1 = self.pri_sh.restart_db_cluster()
        self.log.info(restart_msg1)
        status1 = self.pri_sh.get_db_cluster_status()
        self.assertTrue("Degraded" in status1 or "Normal" in status1,
                        '执行失败:' + step1_txt)

        step2_txt = '----step2: 创建表，expect: 成功----'
        self.log.info(step2_txt)
        sql_cmd2 = f"drop table if exists {self.t_name};" \
                   f"create table {self.t_name} (a int);"
        msg2 = self.pri_sh.execut_db_sql(sql_cmd2)
        self.log.info(msg2)
        self.assertTrue(
            msg2.count(self.constant.CREATE_TABLE_SUCCESS) == 1,
            '执行失败:' + step2_txt)

        step3_txt = '----step3: 创建索引，expect: 成功----'
        self.log.info(step3_txt)
        sql_cmd3 = f"drop index if exists {self.i_name};" \
                   f"create index {self.i_name} on {self.t_name}(a);"
        msg3 = self.pri_sh.execut_db_sql(sql_cmd3)
        self.log.info(msg3)
        self.assertTrue(msg3.count(self.constant.CREATE_INDEX_SUCCESS) == 1,
                        '执行失败:' + step3_txt)

        step4_txt = '----step4: 删除表，expect: 成功----'
        self.log.info(step4_txt)
        sql_cmd4 = f"drop table if exists {self.t_name};"
        msg4 = self.pri_sh.execut_db_sql(sql_cmd4)
        self.log.info(msg4)
        self.assertTrue(
            msg4.count(self.constant.DROP_TABLE_SUCCESS) == 1,
            '执行失败:' + step4_txt)

        step5_txt = '----step5:查询回收站，expect: 成功----'
        self.log.info(step5_txt)
        sql_cmd5 = f"select rcyname,rcyoriginname,rcytablespace from " \
                   f"gs_recyclebin;"
        msg5 = self.pri_sh.execut_db_sql(sql_cmd5)
        self.log.info(msg5)
        self.assertIn("(2 rows)", msg5, '执行失败:' + step5_txt)

        step6_txt = '----step6: purge，expect: 成功----'
        self.log.info(step6_txt)
        sql_cmd6 = f"purge recyclebin;"
        msg6 = self.pri_sh.execut_db_sql(sql_cmd6)
        self.log.info(msg6)
        self.assertIn('PURGE RECYCLEBIN', msg6, '执行失败:' + step6_txt)

        step7_txt = '----step7:查询回收站，expect: 成功----'
        self.log.info(step7_txt)
        sql_cmd7 = f"select rcyname,rcyoriginname,rcytablespace from " \
                   f"gs_recyclebin;"
        msg7 = self.pri_sh.execut_db_sql(sql_cmd7)
        self.log.info(msg7)
        self.assertIn("(0 rows)", msg7,  '执行失败:' + step7_txt)

    def tearDown(self):
        self.log.info('----this is teardown----')
        step8_txt = '----step8: 清理环境----'
        self.log.info(step8_txt)
        self.log.info('恢复参数默认值')
        msg8 = self.pri_sh.execute_gsguc('set',
                                         self.constant.GSGUC_SUCCESS_MSG,
                                         f"enable_recyclebin="
                                         f"'{self.default_value1}'")
        self.log.info(msg8)
        self.log.info('恢复参数默认值')
        msg8_1 = self.pri_sh.execute_gsguc('set',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           f"enable_default_ustore_table="
                                           f"'{self.default_value2}'")
        self.log.info(msg8_1)
        restart_msg8 = self.pri_sh.restart_db_cluster()
        self.log.info(restart_msg8)
        self.log.info('删除表和模式')
        drop_sql = f'purge index {self.i_name};purge table {self.t_name};' \
                   f'drop index if exists {self.i_name};' \
                   f'drop table if exists {self.t_name};'
        drop_result = self.pri_sh.execut_db_sql(drop_sql)
        self.log.info(drop_result)
        status8 = self.pri_sh.get_db_cluster_status()
        self.assertTrue(msg8, '执行失败:' + step8_txt)
        self.assertTrue(msg8_1, '执行失败:' + step8_txt)
        self.assertTrue("Degraded" in status8 or "Normal" in status8,
                        '执行失败:' + step8_txt)
        self.assertTrue(
            drop_result.count(self.constant.DROP_INDEX_SUCCESS_MSG) == 1 and
            drop_result.count(self.constant.DROP_TABLE_SUCCESS) == 1,
            '执行失败:' + step8_txt)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
